import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Manager {
    // 存储收入记录
    private static List<Income> incomes = new ArrayList<>();
    // 存储支出记录
    private static List<Expense> expenses = new ArrayList<>();
    // 每月预算限额
    private static double monthlyBudget = 0;
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println();
            System.out.println("==============================");
            System.out.println("    欢迎使用个人账单管理系统     ");
            System.out.println("==============================");
            System.out.println("请选择操作：");
            System.out.println("1.记录收入");
            System.out.println("2.记录支出");
            System.out.println("3.查看所有账单");
            System.out.println("4.查询账单");
            System.out.println("5.设置月度预算");
            System.out.println("6.查看月度统计报告");
            System.out.println("7.退出系统");
            System.out.println();
            System.out.println("请输入选项序号:");
            String choose = sc.next();
            switch (choose) {
                case "1" -> recordIncome();
                case "2" -> recordExpenditures();
                case "3" -> viewAllBills();
                case "4" -> viewBills();
                case "5" -> setMonthlyBudget();
                case "6" -> viewMonthlyReport();
                case "7" -> {
                    System.out.println("结束");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选择");
            }
        }
    }

    private static void recordIncome() {
        System.out.println("请输入收入信息：");
        System.out.print("日期（YYYY-MM-DD）：");
        String date = sc.next();
        System.out.print("金额：");
        double amount = sc.nextDouble();
        if (amount <= 0) {
            System.out.println("收入金额必须为正数，请重新输入。");
            return;
        }
        System.out.print("类别（如工资、奖金等）：");
        String category = sc.next();
        System.out.print("备注：");
        String remark = sc.next();
        Income incomeRecord = new Income(date, amount, category, remark);
        incomes.add(incomeRecord);
        System.out.println("收入已成功记录！");
    }

    private static void recordExpenditures() {
        System.out.println("请输入支出信息：");
        System.out.print("日期（YYYY-MM-DD）：");
        String date = sc.next();
        System.out.print("金额：");
        double amount = sc.nextDouble();
        if (amount <= 0) {
            System.out.println("支出金额必须为正数，请重新输入。");
            return;
        }
        System.out.print("类别（如餐饮、交通、购物等）：");
        String category = sc.next();
        System.out.print("备注：");
        String remark = sc.next();
        Expense expenseRecord = new Expense(date, amount, category, remark);
        expenses.add(expenseRecord);
        System.out.println("支出已成功记录！");
    }

    private static void viewAllBills() {
        System.out.println("收入记录：");
        for (Income record : incomes) {
            System.out.println(record);
        }
        System.out.println("支出记录：");
        for (Expense record : expenses) {
            System.out.println(record);
        }
    }

    private static void viewBills() {
        System.out.println("请选择查询方式：");
        System.out.println("1. 按指定日期查询");
        System.out.println("2. 按日期范围查询");
        System.out.println("3. 按类别查询");
        int queryChoice = sc.nextInt();
        switch (queryChoice) {
            case 1:
                System.out.print("请输入日期（YYYY-MM-DD）：");
                String specificDate = sc.next();
                queryByDate(specificDate);
                break;
            case 2:
                System.out.print("请输入开始日期（YYYY-MM-DD）：");
                String startDate = sc.next();
                System.out.print("请输入结束日期（YYYY-MM-DD）：");
                String endDate = sc.next();
                queryByDateRange(startDate, endDate);
                break;
            case 3:
                System.out.print("请输入类别：");
                String category = sc.next();
                queryByCategory(category);
                break;
            default:
                System.out.println("无效的查询方式，请重新输入。");
        }
    }

    private static void queryByDate(String date) {
        System.out.println("收入记录：");
        for (Income record : incomes) {
            if (record.getDate().equals(date)) {
                System.out.println(record);
            }
        }
        System.out.println("支出记录：");
        for (Expense record : expenses) {
            if (record.getDate().equals(date)) {
                System.out.println(record);
            }
        }
    }

    private static void queryByDateRange(String startDate, String endDate) {
        System.out.println("收入记录：");
        for (Income record : incomes) {
            if (record.getDate().compareTo(startDate) >= 0 && record.getDate().compareTo(endDate) <= 0) {
                System.out.println(record);
            }
        }
        System.out.println("支出记录：");
        for (Expense record : expenses) {
            if (record.getDate().compareTo(startDate) >= 0 && record.getDate().compareTo(endDate) <= 0) {
                System.out.println(record);
            }
        }
    }

    private static void queryByCategory(String category) {
        System.out.println("收入记录：");
        for (Income record : incomes) {
            if (record.getCategory().equals(category)) {
                System.out.println(record);
            }
        }
        System.out.println("支出记录：");
        for (Expense record : expenses) {
            if (record.getCategory().equals(category)) {
                System.out.println(record);
            }
        }
    }

    private static void setMonthlyBudget() {
        System.out.print("请输入每月支出的预算限额：");
        monthlyBudget = sc.nextDouble();
        System.out.println("月度预算已设置成功，预算限额为：" + monthlyBudget);
    }

    private static void viewMonthlyReport() {
        double totalIncome = 0;
        double totalExpense = 0;
        System.out.println("月度统计报告：");
        System.out.println("总收入：");
        for (Income record : incomes) {
            totalIncome += record.getAmount();
        }
        System.out.println(totalIncome);
        System.out.println("总支出：");
        for (Expense record : expenses) {
            totalExpense += record.getAmount();
        }
        System.out.println(totalExpense);
        System.out.println("各类别支出金额：");
        for (Expense record : expenses) {
            System.out.println(record.getCategory() + "：" + record.getAmount());
        }
        System.out.println("当前剩余可用预算：" + (monthlyBudget - totalExpense));
    }
}

